package com.xuhai.blog.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xuhai.blog.admin.entity.CommentEntity;
import com.xuhai.blog.admin.mapper.CommentMapper;
import com.xuhai.blog.admin.service.CommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 评论表 服务实现类
 * </p>
 *
 * @author 机蜂
 * @since 2022-02-08
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, CommentEntity> implements CommentService {

    @Override
    public void deleteByArticleId(Integer articleId) {
        this.remove(new QueryWrapper<CommentEntity>().eq("article_id",articleId));
    }

    @Override
    public IPage<CommentEntity> getPage(int page, int limit, CommentEntity comment) {
        String startTime=null;
        String endTime=null;
        if(StringUtils.isNotBlank(comment.getCommentTimeRange())){
            startTime=comment.getCommentTimeRange().split(",")[0];
            endTime=comment.getCommentTimeRange().split(",")[1];
        }
        return this.page(new Page<>(page,limit),new QueryWrapper<CommentEntity>()
                .eq(StringUtils.isNotBlank(comment.getName()),"name",comment.getName())
                .eq(comment.getCommentStatus() != null,"comment_status",comment.getCommentStatus())
                .eq(comment.getStatus() != null,"status",comment.getStatus())
                .eq(comment.getArticleId()!=null,"article_id",comment.getArticleId())
                .between(StringUtils.isNotBlank(comment.getCommentTimeRange()),"comment_time",startTime,endTime)
                .orderByDesc("comment_time")
        );
    }
}
